<?php

namespace App\Models\Member;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserUni extends Model
{
    use HasFactory;
    protected $table = 'user_uni';
    protected $primaryKey = 'user_id';
    public $timestamps = false;
    protected $fillable = ['user_id', 'mp_openid','dev_openid','min_openid','nickname','sex', 'city', 'province', 'headimgurl', 'wx_unionid'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    static public function adduser(string $type,$data)
    {
        $user = User::api_reg();
        if($user){
            if($type=='min'){
                self::updateOrCreate(['user_id'=>$user->id],[
                    'min_openid'=>$data['openid'],
                    'nickname'=>$data['nickName'],
                    'sex'=>$data['gender'],
                    'city'=>$data['city'],
                    'province'=>$data['province'],
                    'headimgurl'=>$data['avatarUrl'],
                    'wx_unionid'=>isset($data['unionid'])?$data['unionid']:'',
                ]);
            }else if($type=='dev'){
                self::updateOrCreate(['user_id'=>$user->id],[
                    'dev_openid'=>$data['openid'],
                    'nickname'=>$data['nickname'],
                    'wx_unionid'=>isset($data['unionid'])?$data['unionid']:'',
                ]);
            }else if($type=='mp') {
                //self::updateOrCreate(['user_id' => $user->id, 'mp_openid' => $username]);
            }else if($type=='qq') {
                self::updateOrCreate(['user_id'=>$user->id],[
                    'qq_openid'=>$data['openid'],
                    'nickname'=>$data['nickname'],
                    'qq_unionid'=>isset($data['unionid'])?$data['unionid']:'',
                ]);
            }
            $user->urlAvatar($data['avatarUrl'],'avatar');
            Common::resJson([
                'code'=> 10021,
                'msg' => '登录成功',
                'data' => $user->api_user_data()
            ]);
        }else{
            Common::resJson([
                'code'=> 10024,
                'msg' => '登录错误',
                'data' => $user->api_user_data()
            ]);
        }
    }

}
